import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JButton;
import java.awt.Scrollbar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JScrollBar;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class Navigation extends JPanel {

	/**
	 * Create the panel.
	 */
	private JPanel displayContainer;
	private JList sessionList;
	private Workspace workspace;
	private int displayIndex;
	private JScrollBar scrollBar;
	private IconResizer resizer;
	private int selectionHistory;
	
	public Navigation(int x, int y, int width, int height) {
		setLayout(null);
		setBounds(x, y, width, height);
		displayIndex = 0;
		resizer = new IconResizer (null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 10, getWidth() - 20, getHeight() - 20);
		add(tabbedPane);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(0, 0, tabbedPane.getWidth(), tabbedPane.getHeight());
		tabbedPane.addTab("Problem", null, panel, null);
		panel.setLayout(null);
		
		scrollBar = new JScrollBar();
		scrollBar.setBounds(panel.getWidth() - 21, 0, 17, panel.getHeight() - 27);
		scrollBar.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				displayContainer.setLocation(displayContainer.getX(), 0 - scrollBar.getValue());
			}
		});
		scrollBar.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent arg0) {
				displayContainer.setLocation(displayContainer.getX(), 0 - scrollBar.getValue());
			}
		});
		
		scrollBar.setMaximum(0);
		panel.add(scrollBar);
		
		displayContainer = new JPanel();
		displayContainer.setBounds(0, 0, panel.getWidth() - 17, 10);
		displayContainer.setBackground(Color.WHITE);
		displayContainer.setLayout(null);
		panel.add(displayContainer);
		
		sessionList = new JList();
		sessionList.setBounds(0, 0, tabbedPane.getWidth(), tabbedPane.getHeight());
		
		selectionHistory = -1;
		
		tabbedPane.addTab("Session", null, sessionList, null);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{tabbedPane}));
		updateUI();
	}
	
	public void addDisplay(Problem problem){
		displayIndex = displayContainer.getComponentCount();
		final JPanel display = new JPanel ();
		displayContainer.setSize (displayContainer.getWidth(), displayContainer.getHeight() + 110);
		display.setBounds (10, displayContainer.getHeight() - 110, displayContainer.getWidth() - 20, 100);
		display.setBackground(null);
		display.setBorder(new LineBorder(Color.BLACK));
		display.setLayout(null);
		display.setName(displayIndex + "");
		
		JLabel sample = new JLabel ();
		sample.setBorder(new LineBorder(Color.BLACK));
		int yDivision = display.getHeight() / 2; // half of height of Workspace
		int height = (int)(((float)yDivision / 100) * 80); // calculate height of sample and comparisons slots
		int sampleWidth = display.getWidth() / 3; // calculate width of sample slot
		sample.setBounds((display.getWidth() / 2) - (sampleWidth / 2), yDivision / 10, sampleWidth, height); // set bounds for sample slot
		if (!problem.getSample().equals("")){
			 ImageIcon image = new ImageIcon(problem.getSample());
			 resizer.setImage(image);
			 resizer.resize(sample.getHeight(), sample.getWidth());
			 sample.setIcon(resizer.getImage());
		} else {
			 sample.setBackground(null);
		}
		display.add(sample);

		int xDivision = display.getWidth() / problem.getNoOfComparisons();
		for (int i = 0; i < problem.getNoOfComparisons(); i++ ){	
			int x = (xDivision * i) + (xDivision / 10); // calculate X position of comparison slot
			int width = (int)(((float)xDivision / 100) * 80); // calculate width of comparison slot
			JLabel comparison = new JLabel();
			if (problem.getComparison(i).getImage() != null){
				ImageIcon image = new ImageIcon(problem.getComparison(i).getImage());
				resizer.setImage(image);
				resizer.resize(height, width);
				comparison.setIcon(resizer.getImage());
			} else {
				 comparison.setBackground(null);
			}
			comparison.setBounds(x, yDivision + (yDivision / 10), width, height); // set bounds of comparison slot
			comparison.setBorder(new LineBorder(Color.BLACK));
			display.add(comparison);
		}
		display.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				int index = display.getName().charAt(0) - 48;
				workspace.updateProblem();
				workspace.clearWorkspace();
				workspace.loadProblem(index);
				displayIndex = index;
			}
		});
		displayContainer.add(display);
		displayContainer.updateUI();
		updateScroll ();
	}
	
	public void updateSessionList (Test test){
		String[] selections = new String[test.getNoOfSessions()];
		for (int i = 0; i < selections.length; i++){
			selections[i] = test.getSession(i).getName();
		}
		sessionList.setListData(selections);
		sessionList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				if (sessionList.getSelectedIndex() != Navigation.this.getSelectionHistory()){
				   sessionList.setSelectedIndex(sessionList.getSelectedIndex());
				   workspace.updateProblem();
				   workspace.clearWorkspace();
				   if (sessionList.getSelectedIndex() != -1){
				      workspace.loadSession(sessionList.getSelectedIndex());
				   } else {
					  workspace.loadSession(workspace.getSessionIndex());
				   }
				   workspace.loadProblem(0);
				   clearDisplayContainer();
				   Session session = workspace.getSession();
				   for (int i = 0; i < session.getNoOfProblems(); i++){
					   addDisplay(session.getProblem(i));
				   }
				   Navigation.this.setSelectionHistory (sessionList.getSelectedIndex());
				}
			}
		});
	}
	
	public void updateDisplay (String image){
		char c = image.charAt(0);
		int index = c - 48;
		JComponent display = (JComponent) displayContainer.getComponent(displayIndex);
		JLabel slot = (JLabel) display.getComponent(index);
		ImageIcon img = new ImageIcon(image.substring(1));
		resizer.setImage(img);
		resizer.resize(slot.getHeight(), slot.getWidth());
		slot.setIcon(resizer.getImage());
		display.updateUI();
	}
	
	public void addDisplaySlot (){
		JComponent display = (JComponent) displayContainer.getComponent(displayIndex);
		JLabel slot = new JLabel();
		slot.setBackground(null);
		slot.setBorder(new LineBorder(Color.BLACK));
		display.add(slot);
		updateDisplayComparisonSlots (display);
	}
	
	public void removeDisplaySlot (int componentIndex){
		JComponent display = (JComponent) displayContainer.getComponent(displayIndex);
		display.remove(componentIndex);
		updateDisplayComparisonSlots (display);
	}
	
	private void updateDisplayComparisonSlots (JComponent display){
		int xDivision = display.getWidth() / (display.getComponentCount() - 1);
		int yPosition = display.getComponent(1).getY();
		int height = display.getComponent(1).getHeight();
		for (int i = 1; i < display.getComponentCount(); i++ ){	
			JComponent comparison = (JComponent) display.getComponent(i);
			int x = (xDivision * (i - 1)) + (xDivision / 10); // calculate X position of comparison slot
			int width = (int)(((float)xDivision / 100) * 80); // calculate width of comparison slot
			comparison.setBounds(x, yPosition, width, height); // set bounds of comparison slot
		}
		display.updateUI();
	}
	
	public void removeDisplay (int displayIndex){
		displayContainer.remove(displayIndex);
		for (int i = displayIndex; i < displayContainer.getComponentCount(); i++){
			JComponent display = (JComponent) displayContainer.getComponent(i);
			display.setLocation(display.getX(), display.getY() - 110);
		}
		displayContainer.setSize(displayContainer.getWidth(), displayContainer.getHeight() - 110);
		displayContainer.updateUI();
		updateScroll ();
	}
	
	public void clearDisplayContainer (){
		displayContainer.removeAll();
		displayContainer.setSize(displayContainer.getWidth(), 10);
	}
	
	public void setWorkspace (Workspace wks){
		workspace = wks;
	}
	
	public void updateScroll (){
		int maxScroll = displayContainer.getHeight() - scrollBar.getParent().getHeight() + 10;
		if (maxScroll <= 0){
			scrollBar.setMaximum(0);
			scrollBar.updateUI();
		} else {
			scrollBar.setMaximum(maxScroll);
			scrollBar.updateUI();
		}
	}
	
	public void setSelectionHistory (int index){
		selectionHistory = index;
	}
	
	public int getSelectionHistory (){
		return selectionHistory;
	}
}
